package com.mall.author.service.impl;

import com.mall.author.feign.MemberFeignClient;
import com.mall.author.feign.SmsFeignClient;
import com.mall.author.service.LoginService;
import com.mall.author.vo.LoginVo;
import com.mall.author.vo.RegistryVo;
import com.mall.common.constant.AuthorConstant;
import com.mall.common.exception.HttpStatusCode;
import com.mall.common.exception.ValidationException;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author: 谢君臣
 * @Date: 2022/8/6-16:26
 * @version: 1.0
 * @function:
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private SmsFeignClient smsFeignClient;
    @Resource
    private MemberFeignClient memberFeignClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result getSmsCode(String phone) {
        // 存储验证码
        String code = UUID.randomUUID().toString().substring(0, 6);
        String key = AuthorConstant.SMS_CODE_CACHE_PREFIX + phone;
        String value = code + "_" + System.currentTimeMillis();
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String oldValue = ops.get(key);
        if (StringUtils.isNotEmpty(oldValue)) {
            Long time = Long.parseLong(oldValue.split("_")[1]);
            if (System.currentTimeMillis() - time < 60000)
                return new Result().error(HttpStatusCode.SMS_CODE_EXCEPTION.getCode(), HttpStatusCode.SMS_CODE_EXCEPTION.getMsg());
        }
        ops.set(key, value, 10, TimeUnit.MINUTES);
        return smsFeignClient.sendCode(phone, code);
    }

    @Override
    public HashMap<String, String> registry(RegistryVo registryVo) {
        HashMap<String, String> result = new HashMap<>();
        String key = AuthorConstant.SMS_CODE_CACHE_PREFIX + registryVo.getPhone();
        String code = redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(code)){
            // 验证码过期
            result.put("code","验证码过期");
            return result;
        }
        String old_code = code.split("_")[0];
        if (StringUtils.isNotEmpty(old_code) && old_code.equalsIgnoreCase(registryVo.getCode()))
        {
            redisTemplate.delete(key);
            // 远程验证
            Result registry = memberFeignClient.registry(registryVo);
            if (registry.getCode()!=0){
                if (registry.getCode()== HttpStatusCode.PHONE_EXIST_EXCEPTION.getCode())
                    result.put("phone",registry.getMsg());
                else if (registry.getCode()== HttpStatusCode.USER_NAME_EXIST_EXCEPTION.getCode())
                    result.put("userName",registry.getMsg());
                else
                    result.put("registry",registry.getMsg());
                return result;
            }
            return null;
        }else {
            result.put("code","验证码错误");
            return result;
        }
    }

    @Override
    public Result login_pwd(LoginVo loginVo) throws RuntimeException {
        return memberFeignClient.login_pwd(loginVo);

    }
}
